"""          
Time : 2024/5/31 下午3:41
Author : TJC
@FileName: read_yaml.py
@Desc: 读取yaml格式的测试数据，返回的是字典列表
"""
import yaml
from config.config import cm


# class ReadYaml:
#
#     def __init__(self, yaml_file):
#         self.yaml_file = yaml_file
#
#     def read_yaml(self, key):
#         with open(self.yaml_file, 'r', encoding='utf-8') as f:
#             datas = yaml.load(f, Loader=yaml.FullLoader)  # 避免报警告，需要加入Loader=yaml.FullLoader
#             value = datas[key]
#             return value
# yaml_para = ReadYaml(cm.test_web_yaml_files)
# # login_para = yaml_para.read_yaml('login')
# baidu_para = yaml_para.read_yaml('baidu')
# sk_para = yaml_para.read_yaml('sk')

class ReadYaml:
    def __init__(self, yaml_files):
        # 现在接受一个文件列表
        self.yaml_files = yaml_files

    def read_yaml(self, key):
        all_data = {}
        for yaml_file in self.yaml_files:
            with open(yaml_file, 'r', encoding='utf-8') as f:
                # 使用safe_load避免警告，同时累积所有文件的数据
                data = yaml.safe_load(f)
                all_data.update(data)  # 将当前文件的数据合并到总数据中

        # 检查key是否存在于累积的所有数据中
        if key in all_data:
            return all_data[key]
        else:
            raise KeyError(f"Key '{key}' not found in any of the YAML files.")


# 假设cm.test_web_yaml_files现在返回一个文件路径列表
yaml_para = ReadYaml(cm.test_web_yaml_files)
baidu_para = yaml_para.read_yaml('baidu')
sk_para = yaml_para.read_yaml('sk')

"""
返回字典列表 
[{'case1': '登录成功1', 'username': 70001, 'password': '7001@mcx', 'expect': '失败'}, 
{'case2': '登录成功2', 'username': 70002, 'password': None, 'expect': '密码'}, 
{'case3': '登录成功3', 'username': 70003, 'password': 70003, 'expect': '密码', 'desc': '登录失败(用户不存在）'}]
[{'case': '输入搜索邓小平', 'value': '邓小平', 'expect': '邓小平'}]
"""
